package com.terrier.ui.controls
{
    import com.terrier.ui.skins.IWxButtonSkin;
    import com.terrier.ui.skins.IWxSkinBase;
    import com.terrier.ui.skins.WxSkinManager;

    import flash.display.DisplayObject;

    /**
     * 普通按钮 (没有文本标签)
     * @author Jarvis.weng
     */
    public class WxButton extends WxButtonBase
    {
        /**
         * @private
         */
        protected var _currentBg:DisplayObject;

        /**
         * 构造函数
         */
        public function WxButton()
        {
            super();
        }

        /**
         * @private
         */
        override public function setSkinID(skinID:String):void
        {
            setSkin(WxSkinManager.getButtonSkin(skinID));
        }

        /**
         * @private
         */
        override public function setSkin(value:IWxSkinBase):void
        {
            super.setSkin(value);

			updateView();
			updateLayout();
        }

        /**
         * @private
         */
        override protected function updateView():void
        {
            var index:int = 0;
            if (hasChild(_currentBg))
            {
                index = getChildIndex(_currentBg);
                removeChild(_currentBg);
            }

            if (skin == null)
            {
                return;
            }

            _currentBg = getBgByPhase(_phase);

            if (_currentBg != null)
            {
                addChildAt(_currentBg, index);
            }
        }

        /**
         * @private
         */
        protected function getBgByPhase(phase:String):DisplayObject
        {
            var bg:DisplayObject;

            switch (phase)
            {
                case WxButtonPhase.UP:
                    bg = skin.upBg;
                    break;

                case WxButtonPhase.OVER:
                    bg = skin.overBg;
                    break;

                case WxButtonPhase.DOWN:
                    bg = skin.downBg;
                    break;

                case WxButtonPhase.DISABLED:
                    bg = skin.disabledBg;
                    break;
            }

            return bg;
        }

        /**
         * @private
         */
        override protected function abstractDetection():void
        {
            // nothing to do
        }

        private function get skin():IWxButtonSkin
        {
            return _skin as IWxButtonSkin;
        }
    }
}
